import ClientCookie
import urllib, urllib2
from urllib2 import URLError, HTTPError
import time

def GetPage(address, form = {}, encoding="utf-8", method="POST"):
    if type(form) != type({}):
        request = urllib2.Request(address)
    elif str(form) == str({}):
        request = urllib2.Request(address)
    else:
        qstring = urllib.urlencode(form) 
        if method == "GET":
            request = urllib2.Request('%s?%s' % (address,qstring)) 
        else: #default = POST
            request = ClientCookie.Request(address, qstring)

    request.add_header('User-Agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050524 Fedora/1.5 Firefox/1.5')
    count = 0
    while(1):
        try:
            print "GetPage - %s" % address
            response = ClientCookie.urlopen(request)
            if encoding != "":
                page = response.read().decode(encoding)
            else:
                page = response.read()
            return page
        
        except URLError, e:
            if e.reason[0] == 10060:
                count += 4

            if count >= 5:
                break
            count += 1
            time.sleep(10)

    print "GetPage failed"
    return ""

